package com.maycurobj.controller.maycurbase;

import com.maycurobj.entity.bpm.PersonnelInfoDTO;
import com.maycurobj.entity.bpm.SuperiorInfoEntity;
import com.maycurobj.entity.oa.OASuperiorEntity;
import com.maycurobj.service.impl.OAService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 根据OA同步BPM上级
 * 需工号相等、OA工号不为空、BPM工号不为空
 */
@Component
@Slf4j
public class SuperiorSynchronization {
    @Autowired
    private OAService oaService;

    @Deprecated
    public void timedSuperior() {
        List<OASuperiorEntity> superiors = oaService.getSuperiors();
        Map<String, OASuperiorEntity> map1 = new HashMap<>();
        superiors.stream().forEach(e -> {
            map1.put(e.getWorkcode(), e);
        });

        List<PersonnelInfoDTO> personnelInfo = oaService.getPersonnelInfo();
        Map<String, PersonnelInfoDTO> map2 = new HashMap<>();
        personnelInfo.stream().forEach(e -> {
            map2.put(e.getEmployeeNo(), e);
        });

        //循环查找出能匹配上的数据
        List<SuperiorInfoEntity> ret = new ArrayList<>();

        for (Map.Entry<String, OASuperiorEntity> entry : map1.entrySet()) {
            String _id = entry.getKey();
            SuperiorInfoEntity superiorInfoEntity = new SuperiorInfoEntity();
            if (map2.containsKey(_id)) {
                OASuperiorEntity _entity=map1.get(_id);
                PersonnelInfoDTO _entity2=map2.get(_id);
                superiorInfoEntity.setWorkcode(_entity.getWorkcode());
                superiorInfoEntity.setId(_entity2.getId());
                superiorInfoEntity.setShangji(_entity.getShangji());
                superiorInfoEntity.setBpmname(_entity2.getName());
                superiorInfoEntity.setBpmemployeeNo(_entity2.getEmployeeNo());
                superiorInfoEntity.setBpmdepartmentId(_entity2.getDepartmentId());
                superiorInfoEntity.setBpmshangjiid(toBpmshangjiid(map2, _entity.getShangji()));

            }
            ret.add(superiorInfoEntity);

        }
        oaService.updateSuperior(ret);
    }


    private String toBpmshangjiid(Map<String, PersonnelInfoDTO> map, String superId) {
        if(map.containsKey(superId)){
            PersonnelInfoDTO dto=map.get(superId);
            return dto.getId();
        }else{
            return null;
        }
    }

}
